package com.franzliszt.professionalcalcuator;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

public class BinaryConversion extends AppCompatActivity {
    private TextView Reset,Zero,ResultBox,Binary_16,Binary_10,Binary_8,Binary_2;
    private BtnCircle mDivision,mAdd,mSub,mMultiply,mMod,mEqual,mPoint;
    private BtnCircle One,Two,Three,Four,Five,Six,Seven,Eight,Nine;
    private StringBuilder numBuilder = new StringBuilder();
    private boolean isReset = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= 21) {
            View decorView = getWindow().getDecorView();
            decorView.setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE );
            getWindow().setStatusBarColor( Color.TRANSPARENT );
        }
        setContentView(R.layout.activity_binary_conversion);
        InitView();
        InitBtnStatus();
        Listener();
        mEqual.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN){
                    int num = Integer.parseInt(ResultBox.getText().toString());
                    Binary_2.setText(Inversion(Conversion(num,2)));
                    Binary_8.setText(Inversion(Conversion(num,8)));
                    Binary_10.setText(Inversion(Conversion(num,10)));
                    Binary_16.setText(Inversion(Conversion(num,16)));
                }else {

                }
                return true;
            }
        });
    }
    private void InitView(){
        ResultBox = findViewById(R.id.ShowResult);

        Reset = findViewById(R.id.Reset);
        Zero = findViewById(R.id.Zero);

        mAdd = findViewById(R.id.Add);
        mSub = findViewById(R.id.Sub);
        mMultiply = findViewById(R.id.Multiply);
        mDivision = findViewById(R.id.Division);
        mMod = findViewById(R.id.Mod);
        mEqual = findViewById(R.id.Equal);
        mPoint = findViewById(R.id.Point);

        One = findViewById(R.id.One);
        Two = findViewById(R.id.Two);
        Three = findViewById(R.id.Three);
        Four = findViewById(R.id.Four);
        Five = findViewById(R.id.Five);
        Six = findViewById(R.id.Six);
        Seven = findViewById(R.id.Seven);
        Eight = findViewById(R.id.Eight);
        Nine = findViewById(R.id.Nine);

        Binary_16 = findViewById(R.id.Binary_16);
        Binary_10 = findViewById(R.id.Binary_10);
        Binary_8 = findViewById(R.id.Binary_8);
        Binary_2 = findViewById(R.id.Binary_2);

    }
    private int[] Conversion(int num,int binary){
     int[] remainder = new int[255];
     int count = 1;
     do {
         remainder[count++] = num%binary;
         num /= binary;
     } while (num != 0);
     remainder[0] = count;
     return remainder;
    }
    private String Inversion(int[] array){
            StringBuilder builder = new StringBuilder();
        for (int i = array[0]-1; i >=1 ; i--) {
            if (array[i] == 10){
                builder.append("A");
            }else if (array[i] == 11){
                builder.append("B");
            }else if (array[i] == 12){
                builder.append("C");
            }else if (array[i] == 13){
                builder.append("D");
            }else if (array[i] == 14){
                builder.append("E");
            }else if (array[i] == 15){
                builder.append("F");
            }else {
                builder.append(array[i]);
            }
        }
        return builder.toString();
    }
    private void InitBtnStatus(){
        mMod.setEnabled(false);
        mSub.setEnabled(false);
        mAdd.setEnabled(false);
        mMultiply.setEnabled(false);
        mDivision.setEnabled(false);
        mPoint.setEnabled(false);
    }
    private void Listener(){
        NumOnClick numOnClick = new NumOnClick();
        Zero.setOnClickListener(numOnClick);
        One.setOnClickListener(numOnClick);
        Two.setOnClickListener(numOnClick);
        Three.setOnClickListener(numOnClick);
        Four.setOnClickListener(numOnClick);
        Five.setOnClickListener(numOnClick);
        Six.setOnClickListener(numOnClick);
        Seven.setOnClickListener(numOnClick);
        Eight.setOnClickListener(numOnClick);
        Nine.setOnClickListener(numOnClick);
        Reset.setOnClickListener(numOnClick);
        mPoint.setOnClickListener(numOnClick);
    }
    private class NumOnClick implements View.OnClickListener{
        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.Zero:
                    numBuilder.append('0');
                    break;
                case R.id.One:
                    numBuilder.append('1');
                    break;
                case R.id.Two:
                    numBuilder.append('2');
                    break;
                case R.id.Three:
                    numBuilder.append('3');
                    break;
                case R.id.Four:
                    numBuilder.append('4');
                    break;
                case R.id.Five:
                    numBuilder.append('5');
                    break;
                case R.id.Six:
                    numBuilder.append('6');
                    break;
                case R.id.Seven:
                    numBuilder.append('7');
                    break;
                case R.id.Eight:
                    numBuilder.append('8');
                    break;
                case R.id.Nine:
                    numBuilder.append('9');
                    break;
                case R.id.Reset:
                    isReset = true;
                    break;
            }
            if (isReset){
                numBuilder.delete(0,numBuilder.length());
                ResultBox.setText("0");
                isReset = false;
            }else {
                ResultBox.setText(numBuilder.toString());
            }

        }
    }
    public void SwitchMain(View view) {
        startActivity(new Intent(BinaryConversion.this,MainActivity.class));
    }
}
